package se.webbzon.boltzmann.editor.component.itemlist;

import java.awt.Dimension;

import javax.swing.JList;
import javax.swing.JScrollPane;

import se.webbzon.boltzmann.editor.comparator.ItemComparator;
import se.webbzon.boltzmann.editor.component.AbstractComponent;
import se.webbzon.boltzmann.editor.list.NullableSortedListModel;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.item.ItemGroup;

public class ItemList extends AbstractComponent {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = -6328091504404172743L;
	
	// The item list of this component
	private final JList<AbstractItem> itemList;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new item list where no item is selected. **/
	public ItemList() {
		this(null);
	}
	
	/** Creates a new item list where the given item is selected. **/
	public ItemList(AbstractItem item) {
		super("Item");
		ItemGroup itemGroup = ItemGroup.instance();
		Dimension 	cellDimension = new Dimension(256,64),
					listDimension = new Dimension(256,320);
		
		NullableSortedListModel<AbstractItem> model = 
				new NullableSortedListModel<AbstractItem>(
											AbstractItem.class,
											new ItemComparator(),
											itemGroup.getItems());
		
		itemList = new JList<AbstractItem>(model);
		itemList.setSelectedValue(item, true);
		itemList.setCellRenderer(new ItemListCell(cellDimension));
		JScrollPane scrollPane = new JScrollPane(itemList);
		scrollPane.setPreferredSize(listDimension);
		add(scrollPane);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the selecte item from this item list. **/
	public AbstractItem getItem() {
		return itemList.getSelectedValue();
	}

}
